<script setup lang="ts">
// 定义组件名
defineOptions({
  name: 'LoginPage'
})
import { loginByCodeAPI, loginByPasswordAPI } from '@/apis/user'
import { RulesCode, RulesMobile, RulesPassword } from '@/utils/rules'
import { showFailToast, showToast } from 'vant'
import { ref } from 'vue'
import { useUserStore } from '@/stores'
import router from '@/router'
// 定义响应式数据
const mobile = ref('')
const password = ref('')
// 是否勾选
const isArgee = ref(false)
// 是否是 密码登录
const isPass = ref(true)
// 存储验证码
const code = ref('')
// 当表单提交时
const onSubmit = async (val: object) => {
  // console.log('ok', val)
  // 如果用户没有勾选我同意 代码要停止执行
  if (!isArgee.value) return showFailToast('请勾选我同意')
  // let res
  // if (isPass.value) {
  //   // 密码登录
  //   // 发送请求进行登录
  //   res = await loginByPasswordAPI(val)
  // } else {
  //   // 验证码登录
  //   res = await loginByCodeAPI(mobile.value, code.value)
  // }
  const res = isPass.value
    ? await loginByPasswordAPI(val)
    : await loginByCodeAPI(mobile.value, code.value)

  // console.log(res.data, '登录成功的数据')
  // 提示用户
  showToast('登录成功')
  // 存储用户信息
  const store = useUserStore()
  store.setUser(res.data)
  // 跳转页面
  router.push('/user')
}
// 导包
import { useMobileCode } from '@/composables/useMobileCode'
// 调用函数得到函数的返回值 返回值是一个对象 将这个对象进行解构
const { onSend, time, formRef } = useMobileCode(mobile)
// 切换 密码和短信验证码
const onChangePass = () => {
  isPass.value = !isPass.value
  // 重置 重置表单项的验证提示
  formRef.value?.resetValidation()
}

const isShow = ref(false)

// onMounted(() => {
//   QC.Login({
//     btnId: 'qq'
//   })
// })
</script>

<template>
  <div class="login-page">
    <cp-nav-bar
      right-text="注册"
      @click-right="router.push('/register')"
    ></cp-nav-bar>
    <!-- 头部 -->
    <div class="login-head">
      <h3>{{ isPass ? '密码' : '短信验证码' }}登录</h3>
      <a href="javascript:;">
        <span @click="onChangePass"
          >{{ isPass ? '短信验证码' : '密码' }}登录</span
        >
        <van-icon name="arrow"></van-icon>
      </a>
    </div>
    <!-- 表单 -->
    <van-form autocomplete="off" @submit="onSubmit" ref="formRef">
      <van-field
        placeholder="请输入手机号"
        type="tel"
        name="mobile"
        v-model="mobile"
        :rules="RulesMobile"
      ></van-field>
      <van-field
        v-if="isPass"
        placeholder="请输入密码"
        :type="`${isShow ? 'text' : 'password'}`"
        name="password"
        v-model="password"
        :rules="RulesPassword"
      >
        <template #button>
          <cp-icon
            @click="isShow = !isShow"
            :name="`${isShow ? 'login-eye-on' : 'login-eye-off'}`"
            class="icon-eye"
          ></cp-icon>
        </template>
      </van-field>
      <van-field
        v-else
        v-model="code"
        placeholder="请输入验证码"
        :rules="RulesCode"
      >
        <template #button>
          <span class="btn-send" @click="onSend" :class="{ active: time > 0 }">
            {{ time > 0 ? `${time}秒后发送验证码` : '发送验证码' }}
          </span>
        </template>
      </van-field>
      <div class="cp-cell">
        <van-checkbox v-model="isArgee">
          <span>我已同意</span>
          <a href="javascript:;">用户协议</a>
          <span>及</span>
          <a href="javascript:;">隐私条款</a>
        </van-checkbox>
      </div>
      <div class="cp-cell">
        <van-button block round type="primary" native-type="submit"
          >登 录</van-button
        >
      </div>
      <div class="cp-cell">
        <a href="javascript:;">忘记密码？</a>
      </div>
    </van-form>
    <!-- 底部 -->
    <div class="login-other">
      <van-divider>第三方登录</van-divider>
      <a
        class="icon"
        href="https://graph.qq.com/oauth2.0/show?which=Login&display=pc&client_id=102015968&response_type=token&scope=all&redirect_uri=http%3A%2F%2Fconsult-patients.itheima.net%2Flogin%2Fcallback"
      >
        <img src="@/assets/qq.svg" alt="" />
      </a>
    </div>
  </div>
</template>

<style lang="scss" scoped>
@import '@/styles/login.scss';
</style>
